<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Index_Controller
 * @category   Controller
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */

/**
 * Main controller for the application
 *
 * @package    Slidestream_Controller
 * @category   Controller
 */
class Slidestream_IndexController extends Zend_Controller_Action 
{   
          
    /**
     * Shows the slide streams that the user has created
     *
     */
    public function indexAction()
    {
        // @todo right now this lists all slides streams.  Should just list the ones the user has created
        
        $this->view->acl = array(
                               'details' => $this->_helper->hasAccess('details')
                           );
        
        $slideStream = new Slide_Stream();
        
        $this->view->slideStreams = $slideStream->fetchAll(null, 'title');
        
        $this->_helper->pageTitle('slidestream-index-index:title');
    }
    
    /**
     * Lets the user create a new slide stream
     */
    public function addAction()
    {
        $slideStream = new Slide_Stream();
        
        $messages = array();
        
        $form = $slideStream->form();
        
        
        if ($this->_request->isPost()) {
                
            if ($form->isValid($_POST)) {
                
                $data = array(
                            'title' => $form->getValue('title')
                        );
                
                $slideStreamId = $slideStream->insert($data);
                
                $this->_helper->flashMessenger->addMessage('Slide Stream created successfully!');
                $this->_helper->redirector->gotoUrl('/slidestream/index/details?slideStreamId=' . $slideStreamId);
            } else {
                $messages[] = "There was a problem submitting your form";
            }
        }
        
        $this->view->messages = $messages;
        $this->view->form = $form; 
        $this->_helper->pageTitle('slidestream-index-add:title');
    }
    
    /**
     * The main slide stream editor.  This is the bee's knees!
     */
    public function detailsAction()
    {
        $get = Zend_Registry::get('getFilter');
        
        if (!isset($get->slideStreamId)) {
            throw new Ot_Exception_Input('No slide steram ID given');
        }
        
        $slideStreamId = $get->slideStreamId;
        
        $slideStream = new Slide_Stream();
        
        $thisSlideStream = $slideStream->find($slideStreamId);
        
        if (is_null($thisSlideStream)) {
            throw new Ot_Exception_Data('Slide Stream not found');
        }
        
        $this->view->slideStream = $thisSlideStream;
        
        $slideStreamMap = new Slide_Stream_Map();
        
        $this->view->entities = $slideStreamMap->getEntitiesForSlideStream($slideStreamId);
        $this->view->messages = $this->_helper->flashMessenger->getMessages();
    }
}